//
//  HomeHeadView.m
//  meituan
//
//  Created by huiwenjiaoyu on 15/12/15.
//  Copyright © 2015年 Rick. All rights reserved.
//

#import "HomeHeadView.h"
#import "HomeHeadCell.h"

#define kHeadViewCell @"homeCollectionViewCell"

@interface HomeHeadView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation HomeHeadView

- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        
        CGFloat itemWidth = kScreenWidth * 0.25;
        CGFloat itemHeight = itemWidth * 0.7;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, itemHeight * 2)
                                             collectionViewLayout:flowLayout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        
        [_collectionView registerClass:[HomeHeadCell class]
            forCellWithReuseIdentifier:kHeadViewCell];
        [self addSubview:_collectionView];
    }
    return _collectionView;
}

- (UIPageControl *)pageControl
{
    if (_pageControl == nil) {
        
        CGSize size = [_pageControl sizeForNumberOfPages:2];
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(kScreenWidth * 0.5, CGRectGetMaxY(self.frame) - 20, size.width, size.height)];
        _pageControl.numberOfPages = 2;
        [self addSubview:_pageControl];
    }
    return _pageControl;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self collectionView];
        [self pageControl];
    }
    return self;
}

- (void)setInfos:(NSArray *)infos
{
    if (_infos != infos) {
        _infos = infos;
        
        [self.collectionView reloadData];
    }
}

#pragma mark - collectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.infos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeHeadCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kHeadViewCell forIndexPath:indexPath];
    cell.iconInfo = self.infos[indexPath.item];
    cell.controlTag = indexPath.item;
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth;
}

@end
